package org.paradigmshift.timelapse

import java.util.prefs.Preferences

object TimeLapsePreferences {
  private val USE_JQL_KEY = "useJql"
  private val DEFAULT_SEARCH_KEY = "defaultSearch"
  private val RESULT_SET_SIZE_KEY = "resultSetSize"
  private val SCHEME_KEY = "scheme"
  private val HOST_KEY = "host"
  private val PORT_KEY = "port"
  private val USERNAME_KEY = "username"
  
  private val prefs = Preferences.userNodeForPackage( TimeLapseApp.getClass )
  
  private var useJql = prefs.getBoolean( USE_JQL_KEY, false )
  private var defaultSearch = prefs.get( DEFAULT_SEARCH_KEY, "" )
  private var resultSetSize = prefs.getInt( RESULT_SET_SIZE_KEY, 15 )
  private var scheme = prefs.get( SCHEME_KEY, "http" )
  private var host = prefs.get( HOST_KEY, "localhost" )
  private var port = prefs.getInt( PORT_KEY, 8080 )
  private var username = prefs.get( USERNAME_KEY, "" )
  
  def getPreferences = prefs
  
  def getUseJql = useJql
  def setUseJql( useJql:Boolean ) {
    this.useJql = useJql
    getPreferences.putBoolean( USE_JQL_KEY, useJql )
    syncChanges
  }
  
  def getDefaultSearch = defaultSearch
  def setDefaultSearch( defaultSearch:String ) {
    this.defaultSearch = defaultSearch
    getPreferences.put( DEFAULT_SEARCH_KEY, defaultSearch )
    syncChanges
  }
  
  def getResultSetSize = resultSetSize
  def setResultSetSize( resultSetSize:Int ) {
    this.resultSetSize = resultSetSize
    getPreferences.putInt( RESULT_SET_SIZE_KEY, resultSetSize )
    syncChanges
  }
  
  def getScheme = scheme
  def setScheme( scheme:String ) {
    this.scheme = scheme
    getPreferences.put( SCHEME_KEY, scheme )
    syncChanges
  }
  
  def getHost = host
  def setHost( host:String ) {
    this.host = host
    getPreferences.put( HOST_KEY, host )
    syncChanges
  }
  
  def getPort = port
  def setPort( port:Int ) {
    this.port = port
    getPreferences.putInt( PORT_KEY, port )
    syncChanges
  }
  
  def getUsername = username
  def setUsername( username:String ) {
    this.username = username
    getPreferences.put( USERNAME_KEY, username )
    syncChanges
  }
  
  def syncChanges() {
    val preferences = getPreferences
    preferences.flush
    preferences.sync
  }
}